package cc.rengu.redp.bizimpl.csmc.controller;


import cc.rengu.redp.bizimpl.csmc.domain.CClearRule;
import cc.rengu.redp.bizimpl.csmc.service.ICClearRuleService;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author lizk
 */
@RestController
@Slf4j
@RequestMapping("/admin/csmc/clearRule")
public class CClearRuleController extends BaseController {

    @Autowired
    private ICClearRuleService clearRuleService;

    @GetMapping
    public Map<String, Object> findByPage(QueryRequest queryRequest, CClearRule clearRule) {
        return getDataTable(clearRuleService.findByPage(queryRequest, clearRule));
    }

    @Log("新增清分规则")
    @PostMapping
//    @RequiresPermissions("brand:add")
    public void createClearRule(@Valid CClearRule clearRule) throws RedpException {

        try {
            this.clearRuleService.createClearRule(clearRule);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }

    }


    @Log("修改/停用/启用/注销清分规则")
    @PutMapping
//    @RequiresPermissions("brand:update")
    public void updateSettleRule(CClearRule clearRule) throws RedpException {
        try {
            this.clearRuleService.updateClearRule(clearRule);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }
    }


    @GetMapping("clearRuleDialog")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "参数", required = true, dataType = "String")
    })
    public Map<String, Object> searchClearRuleData(String q, QueryRequest request){
        return clearRuleService.searchClearRuleData(q, request);
    }


}
